package com.vecspace.security.model;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vecspace.web.HttpContextThreadLocal;

/**获取或设置基于Session的值
 * 
 * @version 1.0
 * @author wanghua
 * @reviewer  
 * @createTime 2021年9月30日 下午3:45:36
 */
public class HttpSessionContext {
	private static Logger logger = LoggerFactory.getLogger(HttpSessionContext.class);
	
	/**
	 * 判断用户是否已经登录
	 * @return 
	 * @author  wanghua
	 */
	public static boolean isLogon(){
		try {
			HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
			String loginToken = (String)session.getAttribute(SSOConsts.SESSION_NAME_LOGIN_TOKEN);
			return loginToken != null && loginToken.length() > 0;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return false;
		}
	}
	
	/**在session中记录当前request的登录用户的id(TM_AUTH_USER.ID),loginId,,userName，
	 * 
	 * @param userId
	 * @param loginId
	 * @param userName 
	 * @author  wanghua
	 */
	public static void setLoginUser(Long userId, String loginId, String userName) {
		setLoginAuthUserId(userId);
		setLoginId(loginId);
		setLoginUserName(userName);
	}
	
	/**
	 * 从session中获取当前登录用户的loginId
	 * @return
	 */
	public static String getLoginId(){
		try {
			HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
			String loginId = (String)session.getAttribute(SSOConsts.SESSION_NAME_LOGIN_ID);
			if(loginId == null || loginId.length() == 0) {
				logger.error("没有获取到loginId");
			}
			return loginId;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}
	/**
	 * 在session中记录当前登录用户的loginId
	 * @return
	 */
	public static void setLoginId(String loginId){
		try {
			HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
			session.setAttribute(SSOConsts.SESSION_NAME_LOGIN_ID, loginId);			
		} catch (Exception e) {
			logger.error(e.getMessage());			
		}
	}

	/**从session中获取当前登录用户的loginId，同getLoginId
	 * 
	 * @return 
	 * @author  wanghua
	 */
	public static String getLoginUserId(){
		return getLoginId();
	}
	
	
	/**
	 * 从session中获取当前request的登录用户的id，TM_AUTH_USER.ID
	 * @return
	 */
	public static Long getLoginAuthUserId(){
		try {
			HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
			Long userId = (Long)session.getAttribute(SSOConsts.SESSION_NAME_LOGIN_USER_ID);
			if(userId == null) {
				logger.error("没有获取到登录用户的id，auth_user.id");
			}
			return userId;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}
	
	/**
	 * 在session中记录当前request的登录用户的id，TM_AUTH_USER.ID
	 * @return
	 */
	public static void setLoginAuthUserId(Long userId){
		try {
			HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
			session.setAttribute(SSOConsts.SESSION_NAME_LOGIN_USER_ID, userId);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * 从session中获取当前登录用户的用户名
	 * @return
	 */
	public static String getLoginUserName(){
		try {
			HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
			String loginId = (String)session.getAttribute(SSOConsts.SESSION_NAME_LOGIN_NAME);
			return loginId;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}
	
	/**
	 * 在session中记录当前登录用户的用户名
	 * @return
	 */
	public static void setLoginUserName(String userName){
		try {
			HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
			session.setAttribute(SSOConsts.SESSION_NAME_LOGIN_NAME, userName);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * 从session中获取当前request的登录用户的租户id（用户只能选择其中一个租户登录，切换租户后原登录的租户失效）
	 * @return
	 */
	public static Long getTenantId(){
		try {
			HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
			Long tenantId = (Long)session.getAttribute(SSOConsts.SESSION_NAME_TENANT_ID);
			if(tenantId == null) {
				logger.error("没有获取到登录用户的租户id");
			}
			return tenantId;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	/**
	 * 在session中记录当前request的登录用户的租户id
	 * @return
	 */
	public static void setTenantId(Long tenantId){
		HttpSession session = HttpContextThreadLocal.getHttpContext().getHttpSession();
		session.setAttribute(SSOConsts.SESSION_NAME_TENANT_ID, tenantId);
	}
}
